package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.Announcement;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 用户公告列表适配器 - 用于显示公告列表，只能查看不能编辑
 */
public class UserAnnouncementAdapter extends RecyclerView.Adapter<UserAnnouncementAdapter.AnnouncementViewHolder> {

    private Context context;
    private List<Announcement> announcementList;
    private OnAnnouncementClickListener listener;

    public interface OnAnnouncementClickListener {
        void onAnnouncementClick(Announcement announcement);
    }

    public UserAnnouncementAdapter(Context context, List<Announcement> announcementList, OnAnnouncementClickListener listener) {
        this.context = context;
        this.announcementList = announcementList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public AnnouncementViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user_announcement, parent, false);
        return new AnnouncementViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AnnouncementViewHolder holder, int position) {
        Announcement announcement = announcementList.get(position);
        
        // 设置公告标题
        holder.tvTitle.setText(announcement.getTitle());
        
        // 设置公告简介（截取部分内容）
        String content = announcement.getContent();
        if (content != null && content.length() > 50) {
            content = content.substring(0, 50) + "...";
        }
        holder.tvContent.setText(content);
        
        // 设置发布时间
        try {
            LocalDateTime dateTime = LocalDateTime.parse(announcement.getPublishTime());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            holder.tvPublishTime.setText(dateTime.format(formatter));
        } catch (Exception e) {
            holder.tvPublishTime.setText(announcement.getPublishTime());
        }
        
        // 设置置顶标志
        if (announcement.isTop()) {
            holder.tvTop.setVisibility(View.VISIBLE);
        } else {
            holder.tvTop.setVisibility(View.GONE);
        }
        
        // 设置点击事件
        holder.cardView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onAnnouncementClick(announcement);
            }
        });
    }

    @Override
    public int getItemCount() {
        return announcementList.size();
    }

    public class AnnouncementViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView tvTitle;
        TextView tvContent;
        TextView tvPublishTime;
        TextView tvTop;

        public AnnouncementViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvPublishTime = itemView.findViewById(R.id.tv_publish_time);
            tvTop = itemView.findViewById(R.id.tv_top);
        }
    }
} 